package com.dmjj.cad.controller;

import com.dmjj.cad.dto.SubgraphDTO;
import com.dmjj.cad.service.SubgraphService;
import com.dmjj.cad.vo.SubgraphVO;
import com.dmjj.cadcommon.base.BaseController;
import com.dmjj.cadcommon.result.ResultEnum;
import com.dmjj.cadcommon.result.ResultObject;
import com.dmjj.cadcommon.util.ListCopyToVOUtil;
import com.dmjj.cadcommon.util.PaginationVOUtil;
import com.dmjj.cadcommon.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @Description 元素子表控制器
 * @Author zhangxue
 * @Date 2019-04-29 15:17:18
 */

@RestController
@Api(tags = "元素子表控制器")
@RequestMapping("subgraph")
public class SubgraphController extends BaseController {

    private static final Log LOGGER = LogFactory.getLog(SubgraphController.class);

    @Autowired
    private SubgraphService service;
    @Autowired
    private ResultObject resultObject;

    /**
	 * 元素子表列表
     * @Author zhangxue
     * @Date 2019-04-29 15:17:18
	 * @Param
	 * @Return
	 */
    @ApiOperation(value = "元素子表列表", response = SubgraphVO.class)
    @PostMapping("list4Page")
    public ResultVO<Object> list4Page(
            SubgraphVO entity,
            @ApiParam(value = "当前页", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页显示条数", required = true) @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize
    ){
        try {
            SubgraphDTO entityDTO = new SubgraphDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<SubgraphDTO> list = service.list4Page(entityDTO, pageNum, pageSize);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), PaginationVOUtil.getPaginationVO(list, SubgraphVO.class, pageNum));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 元素子表列表（无分页）
     * @Author zhangxue
     * @Date 2019-04-29 15:17:18
     * @Param
     * @Return
     */
    @ApiOperation(value = "元素子表列表（无分页）", response = SubgraphVO.class)
    @PostMapping("listByCondition")
    public ResultVO<Object> listByCondition(SubgraphVO entity){
        try {
            SubgraphDTO entityDTO = new SubgraphDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<SubgraphDTO> list = service.listByCondition(entityDTO);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), ListCopyToVOUtil.getListToVO(list, SubgraphVO.class));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 元素子表详细信息
     * @Author zhangxue
     * @Date 2019-04-29 15:17:18
     * @Param
     * @Return
     */
    @ApiOperation(value = "元素子表详细信息", response = SubgraphVO.class)
    @PostMapping("queryOneByPk")
    public ResultVO<Object> queryOneByPk(@ApiParam(value = "id",required = true) @RequestParam(value = "id") Long id){
        try {
            SubgraphDTO entityDTO = service.queryOneByPk(id);
            SubgraphVO entity = new SubgraphVO();
            BeanUtils.copyProperties(entityDTO, entity);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), entity);
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 修改/新增元素子表信息
     * @Author zhangxue
     * @Date 2019-04-29 15:17:18
     * @Param
     * @Return
     */
    @ApiOperation(value = "保存元素子表信息", response = ResultVO.class)
    @PostMapping("update")
    public ResultVO<Object> save(SubgraphVO entity) {
        try {
            Integer id = entity.getId();
            SubgraphDTO entityDTO = new SubgraphDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            if(id==null){
                if(service.save(entityDTO) > 0) {
                    return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(),entityDTO.getId());
                }else {
                    return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
                }
            }else{
                if(service.edit(entityDTO) > 0) {
                    return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(),entityDTO.getId());
                }else {
                    return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 删除元素子表信息
     * @Author zhangxue
     * @Date 2019-04-29 15:17:18
     * @Param
     * @Return
     */
    @ApiOperation(value = "删除元素子表信息", response = ResultVO.class)
    @PostMapping("delete")
    public ResultVO<Object> delete(@ApiParam(value = "id",required = true) @RequestParam(value = "id") Long id){
        try {
            if(service.delete(id) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.success(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }


    /**
     * @Author zhangxue
     * @Description //TODO 查询子元素列表
     * @Date 15:23 2019/4/29
     * @Param
     * @return
     **/
    @ApiOperation(value = "查询子元素列表", response = SubgraphVO.class)
    @PostMapping("byGraphId")
    public ResultVO<Object> byGraphId(@ApiParam(value = "graphId",required = true) @RequestParam(value = "graphId") Long graphId){
        try {
            List<SubgraphDTO> list = service.byGraphId(graphId);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), list);
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }


}